import {Notification, MessageBox} from 'element-ui';
import {getMaxZIndex} from '@/components/mydialog/mydialogUtils';
import {createUUID} from '@/utils/index';

export function successAlert(str) {
    Notification.success(str);
}

export function successAlertNerverStop(str) {
    Notification.success({
        message:str,
        duration:0
    });
}

export const alertSuccess = successAlert;


export function failAlert(str,isshow=true,closeall=5) {
    if (!str) {
        return false;
    }
    console.warn(str)
    if(isshow){
        if(closeall==5){
            Notification&&Notification.closeAll();
        }
      Notification.warning(str);
    }

}
export function alertFailWithHtml(_html) {
  if (!_html) {
    return false;
  }
  Notification&&Notification.closeAll();
  Notification.warning({
    dangerouslyUseHTMLString: true,
    message: _html
  });
}

export const alertFail = failAlert;

export function alertConfirm(message, options = {
    customClass: 'customClassAlertConfirm',
    confirmButtonText: "确定",
}) {
    var customClass = options && options.customClass;
    if (!customClass) {
        customClass = `customClassAlertConfirm${createUUID()}`;
    }
    setTimeout(() => {
        $(`.${customClass}`).parent('.el-message-box__wrapper').css({zIndex: getMaxZIndex()});
    }, 300);

    return MessageBox.confirm(message, options?.title||'提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',

        type: 'warning',
        ...options,
        customClass
    });
}

export async function alertConfirmPlus(message, options = {
  customClass: 'customClassAlertConfirm',
  confirmButtonText: "确定",
}) {
  var customClass = options && options.customClass;
  if (!customClass) {
    customClass = `customClassAlertConfirm${createUUID()}`;
  }
  setTimeout(() => {
    $(`.${customClass}`).parent('.el-message-box__wrapper').css({zIndex: getMaxZIndex()});
  }, 300);

  var result=await  MessageBox.confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    ...options,
    customClass
  }).catch(e=>{
    console.log('拒绝',e)
  return {success:false}
  });
  if(result=='confirm'){
   return {success:true}
  }
  if(! result?.success){
   return {success:false}
  }
  return {success:true}
}


export function alertWithPaylod(payload) {
    if (payload && payload.message) {
        failAlert(payload.message);
        return payload;
    }

    if (payload?.success) {
        successAlert('操作成功');
    }

    return payload;
}

export function alertPrompt(str) {
    return new Promise(resolve => {
        MessageBox.prompt(str || '请输入内容', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }).then(({value}) => {
            resolve(value)
        });
    })
}

export function  throwError(message){
    if(message){
        alertFail(message)
    }

    return Promise.reject(message)
}
